package com.gray.blog.common.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gray.blog.common.result.BasePageResult;
import com.gray.blog.common.result.BaseResult;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;

/**
 * Created  By zhoux 2019/05/29
 **/
public class PagerUtil<T extends BasePageResult> {

    /**
     * 设置分页结果
     */
    public T setPageResultByPageObject(IPage<?> pager, T pageResult, Class<?> copyData) {
        pageResult.setPageNum(pager.getCurrent());
        pageResult.setPageSize(pager.getSize());
        pageResult.setTotalNum(pager.getTotal());
        pageResult.setTotalPage(pager.getPages());
        if (CollectionUtils.isNotEmpty(pager.getRecords())) {
            pageResult.setDataList(BeanUtil.copy(pager.getRecords(), copyData));
        }
        return pageResult;
    }

    /**
     * 设置结果集时间
     */
    public void setPageResultTime(List<? extends BaseResult> data) {
        if (CollectionUtils.isNotEmpty(data)) {
            data.forEach(result -> {
                result.setCreateTimeStamp(result.getCreateDate() != null ? result.getCreateDate().getTime() : null);
                result.setUpdateTimeStamp(result.getUpdateDate() != null ? result.getUpdateDate().getTime() : null);
                result.setLoginTimeStamp(result.getLoginDate() != null ? result.getLoginDate().getTime() : null);
            });
        }
    }

}
